/*
 * Class Organ.java
 * 
 * Abstract class which represents basic characteristics of 
 * human organ (like organ health).
 */

package humanMirror.model.human;

/**
 * Basic class of human organ. It has only common for all organs characteristics
 * and methods.
 *
 * @author Vasyl Khyzhniak
 * @author Anton Samarsky
 * @version 2.0 26 jul 2012
 */
public class Organ {
    private int id;
    protected boolean isNecessaryForLife = true; // is the organ necessary for being
    // alive
    private OneOrganInfo parameters;

    public static final int UPPER_LIMIT = 100; // upper limit of organ
    // characteristics
    public static final int LOWER_LIMIT = 0; // lower limit of organ
    // characteristics

    public Organ(int id, OneOrganInfo organParams) {
        this.id = id;
        parameters = organParams.copy();
    }

    public int getID() {
        return id;
    }

    private void setParameter(int paramNumber, int value) {
        parameters.set(paramNumber, value);
    }

    private int checkedParameter(OneOrganInfo changeParams, int paramNumber) {
        return checkLimits(accumulateParameter(changeParams, paramNumber));
    }

    private int accumulateParameter(OneOrganInfo changeParams, int paramNumber) {
        return changeParams.get(paramNumber) + getParameter(paramNumber);
    }

    private int getParameter(int param) {
        return parameters.get(param);
    }

    public OneOrganInfo getAllParameters() {
        return parameters.copy();
    }

    public boolean getNecessaryForLife() {
        return isNecessaryForLife;
    }

    public void setNecessaryForLife(boolean necessaryForLife) {
        isNecessaryForLife = necessaryForLife;
    }

    /**
     * @param parameterValue defines the change of parameter
     * @return New value for parameter
     */
    protected int checkLimits(int parameterValue) {
        if (parameterValue > UPPER_LIMIT) {
            return UPPER_LIMIT;
        } else if (parameterValue < LOWER_LIMIT) {
            return LOWER_LIMIT;
        } else {
            return parameterValue;
        }
    }

    public void accumulateAllParameterChanges(OneOrganInfo changeParams) {
        for (int paramNumber = 0; paramNumber < parameters.getCountOfParameters(); paramNumber++) {
            setParameter(paramNumber, checkedParameter(changeParams, paramNumber));
        }
    }
}
